var uid,user,name,pic,ws,spage;
document.addEventListener('plusready', function() {	
	uid = plus.storage.getItem('uid');//用户id
	user = plus.storage.getItem('user');//登录手机号
	name = plus.storage.getItem('name') != 'null'?plus.storage.getItem('name'):user;//用户名称
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	console.log(uid+" "+name); 
	//图片上传	
	$('.uploadPhoto').on('tap', function() {
		mui('#sheet1').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		getImage();
	})
	//选择相册添加
	$('.photo').on('tap', function(){
		appendByGalleryMul();
	})
	function getImage() {
		var cmr = plus.camera.getCamera();
		cmr.captureImage(function(path) {
			path = plus.io.convertLocalFileSystemURL(path);
			$('.bbs-pulish-img').prepend('<img src="'+path+'"/>');
		}, function(err) {});
	}
	function appendByGalleryMul() {
		plus.gallery.pick( function(path){
			$('.bbs-pulish-img').prepend('<img src="'+path+'"/>');
		}, function(error){}, {} );
	}
	// 添加照片        
	var upload_pic = []; // 照片数组
	var pic_index = 1;
//写入	
	$("#submit").on('click',function(){
		var content = $("#cont").val();
		upload_pic = []; // 照片数组
		pic_index = 1;
		$('.bbs-pulish-img img[class!=uploadPhoto]').each(function(){
			var p = $(this).attr('src');
			upload_pic.push({name:"uploadkey"+pic_index,path:p});
			pic_index++;
		})
		console.log(upload_pic.length);
		if(!content.trim() && upload_pic.length < 1){
			$("#content").val('');
			toast('没有信息无法发表！');return;
		}
		plus.nativeUI.showWaiting('正在发帖，请稍后...');     
		if(upload_pic.length >= 1){
			var server = apiRoot + '?m=Home&c=Bbs&a=uploadActivityPic';
			var task = plus.uploader.createUpload(server,{method: "POST"},function(t, status) { 
				if (status == 200) {
					pic = $.parseJSON(t.responseText);
					if(pic.msg){
						errortoast(pic.msg);console.log(JSON.stringify(pic));
						return;
					}
					console.log(pic);
					if(pic == -1){
						$('.bbs-pulish-img img[class!=uploadPhoto]').remove();
						toast('上传失败，请重新上传');
						return;
					}
					$.ajax({ 
						type:"get",
						url:apiRoot+'?m=Home&c=Bbs&a=setBbs&uid='+uid+'&name='+name+'&content='+content+'&pic='+pic,
						dataType:"json",
						success:function(data){
							if(data > 0){
								if(spage)plus.webview.getWebviewById(spage).reload();
//								var cur = plus.webview.getWebviewById(spage);
//								cur.evalJS('pullfresh_fun()');
								toast('发布成功');
								setTimeout(function(){ 
									ws.close();
								} , 1500); 
							}
							plus.nativeUI.closeWaiting();
						},
						error:function(e){
							console.log(JSON.stringify(e));
							errortoast();
						}
							
					});
				} else {
					toast('上传图片失败！');
					return;
				}
			});
			for(var i in upload_pic){
				var f=upload_pic[i];
				task.addFile(f.path,{key:f.name});
			} 
			task.start();
		}else{
			$.ajax({ 
				type:"get",
				url:apiRoot+'?m=Home&c=Bbs&a=setBbs&uid='+uid+'&name='+name+'&content='+content,
				dataType:"json",
				success:function(data){
					if(data > 0){
						toast('发布成功');
						if(spage)plus.webview.getWebviewById(spage).reload();
//						var cur = plus.webview.getWebviewById(spage);
//						cur.evalJS('pullfresh_fun()');
						setTimeout(function(){ 
							plus.webview.currentWebview().close();
						} , 1500); 
					}
					plus.nativeUI.closeWaiting();
				},
				error:function(e){
					console.log(JSON.stringify(e));
					errortoast();
				}
			});
		}
	});
}, false);